<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\DateFormat;

class Order extends Model
{
    use HasFactory;
    use DateFormat;
    public $guarded = [];

    const STATUS_UN_BOOK = 0;
    const STATUS_BOOK_OK = 1;
    const STATUS_CONFIRM_OK = 2;
    const STATUS_PHOTO_OK = 3;
    const STATUS_CANCEL_LOCK = 6;
    const STATUS_CANCEL_OK = 7;
    const STATUS_REFUND_LOCK = 8;
    const STATUS_REFUND_OK = 9;


    public function companyinfo() {
        return $this->hasOne(Company::class, 'id', 'company');
    }

    public function goodsinfo() {
        return $this->hasOne(Goods::class, 'id', 'goods');
    }

    public function cardinfo() {
        return $this->hasOne(Card::class, 'id', 'card');
    }

    public function shopinfo() {
        return $this->hasOne(Shop::class, 'id', 'shop');
    }

    public function cityinfo() {
        return $this->hasOne(City::class, 'id', 'city');
    }

    public static function create_card() {
        do{
            $card = str_rand(8);
            $order = self::where('card', $card)->first();
        } while($order);
        return $card;
    }
    public static function reset($id) {
        $order = self::find($id);
        Card::where('order_id', $id)->update(['use_at' => null, 'order_id' => null]);
        $order->update_count = 0;
        $order->status = SELF::STATUS_UN_BOOK;
        $order->shop = 0;
        $order->time = null;
        $order->photoer_name = '';
        $order->photoer_mobile = '';
        $order->confirm_at = null;
        $order->company_card = '';
        $order->save();
    }
}
